iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 27
1
Software Development

如何使用 Linux 從土裡到冒出頭來系列 第 27

Day 27 網路設置與遠端

  • 分享至 

  • xImage
  •  

Day 27 網路設置與遠端

網路及遠端登入可能有先東西在先前已經介紹過,這邊再稍微做個簡單的範例。

指令運用在網路設置

網路設定相關檔案

[root@localhost ~]# ls /etc/sysconfig/network-scripts/

ifcfg-enp0s3  ifdown-ipv6    ifdown-Team      ifup-eth    ifup-post      ifup-tunnel
ifcfg-lo      ifdown-isdn    ifdown-TeamPort  ifup-ippp   ifup-ppp       ifup-wireless
ifdown        ifdown-post    ifdown-tunnel    ifup-ipv6   ifup-routes    init.ipv6-global
ifdown-bnep   ifdown-ppp     ifup             ifup-isdn   ifup-sit       network-functions
ifdown-eth    ifdown-routes  ifup-aliases     ifup-plip   ifup-Team      network-functions-ipv6
ifdown-ippp   ifdown-sit     ifup-bnep        ifup-plusb  ifup-TeamPort

查看網路IP位址

[root@localhost ~]# ifconfig

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::49cd:d69a:1408:7016  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:ef:e7:f0  txqueuelen 1000  (Ethernet)
        RX packets 930  bytes 106715 (104.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 642  bytes 70490 (68.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12  bytes 984 (984.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 984 (984.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

編輯網路卡設定 ifcfg-enp0s3

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

在網路卡設定後面加上

  • IPADDR=192.168.56.1 #靜態IP(自訂)
  • GETAWAY=192.168.56.2 #閘道
  • NETMASK=255.255.255.0 #子網路遮罩
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=803f5bcf-d6a4-4dbc-9fe0-2592121be152
DEVICE=enp0s3
ONBOOT=yes
IPADDR=192.168.56.1   #靜態IP
GETAWAY=192.168.56.2    #閘道
NETMASK=255.255.255.0   #子網路遮罩

重啟網路服務

[root@localhost ~]# systemctl restart network.service

// 或

[root@localhost ~]# service network restart

查看 IP 是否有做更動。

[root@localhost ~]# ifconfig

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.1  netmask 255.255.255.0  broadcast 192.168.55.255
        inet6 fe80::49cd:d69a:1408:7016  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:ef:e7:f0  txqueuelen 1000  (Ethernet)
        RX packets 1462  bytes 168319 (164.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 939  bytes 111426 (108.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 16  bytes 1312 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1312 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以使用 ping 指令來確定網路真的有通。

[root@localhost ~]# ping 192.168.56.1

PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 192.168.56.1: icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from 192.168.56.1: icmp_seq=4 ttl=64 time=0.044 ms
64 bytes from 192.168.56.1: icmp_seq=5 ttl=64 time=0.035 ms
64 bytes from 192.168.56.1: icmp_seq=6 ttl=64 time=0.032 ms
64 bytes from 192.168.56.1: icmp_seq=7 ttl=64 time=0.080 ms
64 bytes from 192.168.56.1: icmp_seq=8 ttl=64 time=0.049 ms
64 bytes from 192.168.56.1: icmp_seq=9 ttl=64 time=0.032 ms
64 bytes from 192.168.56.1: icmp_seq=10 ttl=64 time=0.053 ms
64 bytes from 192.168.56.1: icmp_seq=11 ttl=64 time=0.080 ms
64 bytes from 192.168.56.1: icmp_seq=12 ttl=64 time=0.078 ms
64 bytes from 192.168.56.1: icmp_seq=13 ttl=64 time=0.077 ms
64 bytes from 192.168.56.1: icmp_seq=14 ttl=64 time=0.076 ms
64 bytes from 192.168.56.1: icmp_seq=15 ttl=64 time=0.081 ms
64 bytes from 192.168.56.1: icmp_seq=16 ttl=64 time=0.066 ms
64 bytes from 192.168.56.1: icmp_seq=17 ttl=64 time=0.082 ms
64 bytes from 192.168.56.1: icmp_seq=18 ttl=64 time=0.048 ms
64 bytes from 192.168.56.1: icmp_seq=19 ttl=64 time=0.078 ms
64 bytes from 192.168.56.1: icmp_seq=20 ttl=64 time=0.080 ms
64 bytes from 192.168.56.1: icmp_seq=21 ttl=64 time=0.079 ms
64 bytes from 192.168.56.1: icmp_seq=22 ttl=64 time=0.090 ms
64 bytes from 192.168.56.1: icmp_seq=23 ttl=64 time=0.089 ms
^C
--- 192.168.56.1 ping statistics ---
23 packets transmitted, 23 received, 0% packet loss, time 22844ms
rtt min/avg/max/mdev = 0.032/0.064/0.090/0.019 ms

而 ping 指令也可以對 DNS 做連線測試。

[root@localhost ~]# ping www.thsrc.com.tw

PING e17303.dscb.akamaiedge.net (118.214.247.121) 56(84) bytes of data.
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=1 ttl=53 time=22.9 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=2 ttl=53 time=29.8 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=3 ttl=53 time=20.6 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=4 ttl=53 time=21.9 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=5 ttl=53 time=23.8 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=6 ttl=53 time=21.0 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=7 ttl=53 time=20.5 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=8 ttl=53 time=29.3 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=9 ttl=53 time=23.2 ms
64 bytes from a118-214-247-121.deploy.static.akamaitechnologies.com (118.214.247.121): icmp_seq=10 ttl=53 time=26.5 ms
^C
--- e17303.dscb.akamaiedge.net ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9148ms
rtt min/avg/max/mdev = 20.504/24.001/29.867/3.294 ms

查看自己 IP 位址。

[root@localhost ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:ef:e7:f0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.1/24 brd 192.168.55.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s3
       valid_lft 85720sec preferred_lft 85720sec
    inet6 fe80::49cd:d69a:1408:7016/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

遠端主機登入

ssh 指令在先前有稍微介紹過,他是 Linux 的一種通訊協定工具,利用 ssh 可以遠端登入到網路上的另一台主機。

[root@localhost ~]# ssh [使用者帳號]@[IP位址]

我另外以 Windows 方式做一次。

# 登入遠端主機
sian@DESKTOP ~/Desktop$ ssh root@192.168.56.1
The authenticity of host '192.168.56.1 (192.168.56.1)' can't be established.
ECDSA key fingerprint is SHA256:IpQKuSvoU4n+3PZ+3tTeIX5lONG3UaJIBBDMLlroCKE.

# 是否儲存該主機的辨識字串,打 yes 或 y
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.56.1' (ECDSA) to the list of known hosts.

# 輸入 root 使用者帳號的密碼
root@192.168.56.1's password:
Last login: Mon Oct 12 22:57:55 2020 from gateway

# 之後就進來遠端主機了
[root@localhost ~]#

判別自己是哪個使用者帳號

[root@localhost ~]# whoami
root

而圖形化介面,我們可以設定成這樣

跨主機傳送檔案

scp 全名 secure copy 是利用 ssh 在不同主機進行檔案複製的指令,可由本地端複製傳送到遠端主機。

[root@localhost ~]# scp [要傳送的檔案] [遠端主機帳號]@[遠端主機IP]:[檔案放到哪個路徑]
sian@DESKTOP ~/Desktop$ scp a.tgz root@192.168.56.1:/root/

# 輸入遠端使用者密碼
root@192.168.56.1's password:
a.tgz                                         100% 1412KB  33.9MB/s   00:00



上一篇
Day 26 排程管理
下一篇
Day 28 虛擬機與本機共用資料夾
系列文
如何使用 Linux 從土裡到冒出頭來30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言